﻿using Minecraft.BLL;
using Minecraft.BLL.mysql;
using Minecraft.Config;
using Minecraft.Config.Csv.CsvCls;
using Minecraft.Config.Csv.CsvCom;
using Minecraft.Config.ipConst;
using Minecraft.Model;
using Minecraft.Model.ReqResp;
using SuperSocket.SocketBase.Command;
using SuperSocket.SocketBase.Protocol;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Minecraft.ServerHall.Cmd
{
	public class WeaponSwitch : CommandBase<MinecraftSession, StringRequestInfo>
	{
		public override string Name
		{
			get
			{
				return ProtocolHelper.GetProtocolStr(defCommand);
			}
		}
		private EnumCommand defCommand = EnumCommand.Weapon_WeaponSwitch;
		public override void ExecuteCommand(MinecraftSession session, StringRequestInfo requestInfo)
		{
			var req = requestInfo.GetRequestObj<WeaponSwitchReq>(session);
			if (req == null)
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = $"错误1：（原因：请求实体为空）" });
				return;
			}
			if (!session.minecraftSessionInfo.IsLogin)
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = $"错误1：（原因：未登录）" });
				return;
			}

			if (req.position < 0)
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = $"错误1：（原因：position < 0）" });
				return;
			}

			var weaponInfo = session.minecraftSessionInfo.goodsList.FirstOrDefault(m =>
				m.enumGoodsBelongsToType_Convert == EnumGoodsBelongsToType.ShortcutBar
				&& m.GoodsPosition == req.position
				&& m.guid == "");
			if (weaponInfo == null)
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = $"错误1：（原因：weaponInfo == null）" });
				return;
			}
			Mod_Items mod_Items = CsvConfig.GetMod_ItemsInfoById(weaponInfo.GoodsItemId);
			if (mod_Items == null)
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = $"错误1：（原因：mod_Items == null）" });
				return;
			}
			if (mod_Items.enumGoodsType_Convert != EnumGoodsType.Weapon)
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = $"错误1：（原因：不属于武器）" });
				return;
			}

			switch (mod_Items.modAttributeInfo_Convert.enumAttribute)
			{
				case EnumAttribute.Atk:
					{
						var goodsInfo = session.minecraftSessionInfo.goodsList.FirstOrDefault(m =>
							m.enumGoodsBelongsToType_Convert == EnumGoodsBelongsToType.ShortcutBar
							&& m.GoodsPosition == session.minecraftSessionInfo.player.ShortcutBarWeaponPos
							&& m.guid == "");
						if (goodsInfo != null)
						{
							Mod_Items mod_ItemsWeapon = CsvConfig.GetMod_ItemsInfoById(goodsInfo.GoodsItemId);
							if (mod_ItemsWeapon != null)
							{
								if (mod_ItemsWeapon.enumGoodsType_Convert == EnumGoodsType.Weapon)
								{
									session.minecraftSessionInfo.player.Fight_Attack -= mod_ItemsWeapon.modAttributeInfo_Convert.Value;
								}
							}
						}
						session.minecraftSessionInfo.player.Fight_Attack += mod_Items.modAttributeInfo_Convert.Value;
					}
					break;
				default:
					{
						//session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = $"错误1：（原因：武器切换中，暂不支持该属性【{mod_Items.modAttributeInfo_Convert.Id}:{mod_Items.modAttributeInfo_Convert.enumAttribute.ToString()}】加点）" });
						//return;
					}
					break;
			}
			session.minecraftSessionInfo.player.ShortcutBarWeaponPos = weaponInfo.GoodsPosition;

			List<KeyValueEnumAttributeInt> attributeList = new List<KeyValueEnumAttributeInt>();
			attributeList.Add(new KeyValueEnumAttributeInt { key = EnumAttribute.Atk, value = session.minecraftSessionInfo.player.Fight_Attack });

			var resp = new WeaponSwitchResp
			{
				attributeList = attributeList
			};
			session.Send(defCommand, req, resp);
		}
	}
}
